package iontec.behavior;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class BehaviorEdit extends Activity {
	private EditText mNameText;
	private SeekBar mSeekBar;
	private Integer mValue;
	private Long mRowId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.behavior_ediit);

		mNameText = (EditText) findViewById(R.id.name);
		mSeekBar = (SeekBar)findViewById(R.id.value);

		final TextView seekBarValue = (TextView)findViewById(R.id.out_value);

		mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				seekBarValue.setText(String.valueOf(progress-10));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});
		
		
		Button confirmButton = (Button) findViewById(R.id.submit_b);

		mRowId = null;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String name = extras.getString(GoBDbAdapter.KEY_NAME);
			Integer value = extras.getInt(GoBDbAdapter.KEY_VAL);
			mRowId = extras.getLong(GoBDbAdapter.KEY_ROWID);

			if (name != null) {
				mNameText.setText(name);
			}
			if (value != null) {
				mSeekBar.setProgress(value+10);
			}
		}

		confirmButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Bundle bundle = new Bundle();

				bundle.putString(GoBDbAdapter.KEY_NAME, mNameText.getText().toString());
				bundle.putInt(GoBDbAdapter.KEY_VAL, mSeekBar.getProgress()-10);
				if (mRowId != null) {
					bundle.putLong(GoBDbAdapter.KEY_ROWID, mRowId);
				}

				Intent mIntent = new Intent();
				mIntent.putExtras(bundle);
				setResult(RESULT_OK, mIntent);
				finish();
			}

		}); 

	}
}
